package com.xbfinal.eduorder.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.xbfinal.commonUtils.Exception.GuliException;
import com.xbfinal.commonUtils.JwtUtils;
import com.xbfinal.commonUtils.Result;
import com.xbfinal.eduorder.entity.TOrder;
import com.xbfinal.eduorder.service.TOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author xbfinal
 * @since 2023-03-11
 */
@Api(description = "订单控制")
@RestController
@RequestMapping("/eduorder/order")
public class TOrderController {

    @Autowired
    private TOrderService orderService;


    @ApiOperation("生成订单id的方法")
    @PostMapping("createOrder/{id}")
    public Result creatOrder(@PathVariable("id") String id,
                             HttpServletRequest request) {

        //需要返回订单号
        String idByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if(idByJwtToken==null|| StrUtil.isEmpty(idByJwtToken)){
            return Result.failed().addMessage("请登录");
        }
        String orderNo =  orderService.creatOrders(id,idByJwtToken);
        return Result.ok().add("orderId",orderNo);

    }

    @ApiOperation("根据订单id查询订单信息")
    @GetMapping("getOrderInfo/{id}")
    public Result  getOrderInfo(@PathVariable String id){
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",id);
        TOrder byId = orderService.getOne(wrapper);
        return Result.ok().add("item",byId);
    }

    @ApiOperation("根据课程和用户id查询课堂购买状态")
    @GetMapping("isBudCourse/{courseId}/{userId}")
    public boolean isBudCourse(@PathVariable("courseId") String courseId,
                               @PathVariable("userId") String userId){
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",userId);
        wrapper.eq("status",1);
        int count = orderService.count(wrapper);
        if(count>0){
            return true;
        }

        return false;
    }





}

